﻿using System;
using System.Security.Cryptography;
using System.Text;
using sys = System.Security.Cryptography;

namespace BCCommon
{
    /// <summary>
    /// 此类提供MD5，SHA1，SHA256，SHA384，SHA512等四种算法，字串的长度依次增大。 
    /// </summary>
    public static class HashHelper
    {
        /// <summary>
        /// MD5
        /// </summary>
        /// <param name="inputText">字符串明文</param>
        /// <param name="salt">盐</param>
        /// <returns>字符串密文</returns>
        public static string MD5(this string inputText, string salt = null)
        {
            byte[] bytes;

            using (MD5 md5 = sys.MD5.Create())
            {
                bytes = md5.ComputeHash(Encoding.UTF8.GetBytes((inputText + (salt ?? string.Empty))));
            }

            return BitConverter.ToString(bytes).Replace("-", string.Empty);
        }

        /// <summary>
        /// SHA1
        /// </summary>
        /// <param name="inputText">明文</param>
        /// <param name="salt">盐</param>
        /// <returns>SHA1后字符串</returns>
        public static string SHA1(this string inputText, string salt = null)
        {
            byte[] bytes;

            using (SHA1 sha1 = sys.SHA1.Create())
            {
                bytes = sha1.ComputeHash(Encoding.ASCII.GetBytes(inputText + (salt ?? string.Empty)));
            }

            return BitConverter.ToString(bytes).Replace("-", string.Empty);
        }
        /// <summary>
        /// SHA256
        /// </summary>
        /// <param name="inputText">明文</param>
        /// <param name="salt">盐</param>
        /// <returns>SHA256后字符串</returns>
        public static string SHA256(this string inputText, string salt = null)
        {
            byte[] bytes;

            using (SHA256 sha256 = sys.SHA256.Create())
            {
                bytes = sha256.ComputeHash(Encoding.ASCII.GetBytes(inputText + (salt ?? string.Empty)));
            }

            return BitConverter.ToString(bytes).Replace("-", string.Empty);
        }

        /// <summary>
        /// SHA384
        /// </summary>
        /// <param name="inputText">明文</param>
        /// <param name="salt">盐</param>
        /// <returns>SHA384后字符串</returns>
        public static string SHA384(this string inputText, string salt = null)
        {
            byte[] bytes;

            using (SHA384 sha348 = sys.SHA384.Create())
            {
                bytes = sha348.ComputeHash(Encoding.ASCII.GetBytes(inputText + (salt ?? string.Empty)));
            }

            return BitConverter.ToString(bytes).Replace("-", string.Empty);
        }

        /// <summary>
        /// SHA512
        /// </summary>
        /// <param name="inputText">明文</param>
        /// <param name="salt">盐</param>
        /// <returns>SHA512后字符串</returns>
        public static string SHA512(this string inputText, string salt = null)
        {
            byte[] bytes;

            using (SHA512 sha512 = sys.SHA512.Create())
            {
                bytes = sha512.ComputeHash(Encoding.ASCII.GetBytes(inputText + (salt ?? string.Empty)));
            }

            return BitConverter.ToString(bytes).Replace("-", string.Empty);
        }
    }
}
